import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	// Boolean(JSON.parse(uni.getStorageSync('orderList')))?JSON.parse(uni.getStorageSync('orderList')):
	// Boolean(JSON.parse(uni.getStorageSync('orderData')))?JSON.parse(uni.getStorageSync('orderData')):
	// JSON.parse(uni.getStorageSync('vuexstate')).tabledetail||
	state: {
		tabledetail: uni.getStorageSync('vuexstate1') ? JSON.parse(uni.getStorageSync('vuexstate1')).tabledetail : {},
		orderList: uni.getStorageSync('orderList1') ? JSON.parse(uni.getStorageSync('orderList')) : [],
		orderData: uni.getStorageSync('orderData1') ? JSON.parse(uni.getStorageSync('orderData')) : {
			count: 0,
			money: 0
		},
		dishesIndex: null
	},
	getters:{
		getorderlist(state){
			
		}
	},
	mutations: {
		
		SET_ORDERLIST(state, playload){
			state.orderList=playload
		},
		SET_ORDERDATA(state, playload){
			state.orderData=playload
		},
		SET_STATE(state, playload) {
			let account = JSON.parse(uni.getStorageSync('currentUser')).account
			uni.setStorageSync(account+'vuexstate', JSON.stringify(playload))
		},
		CLEAN_TABLE(state, playload) {
			state.tabledetail = {}

		},
		SET_TABLE(state, playload) {
			// state.tabledetail.tablenumber=playload[1]
			Vue.set(state.tabledetail, 'tablenumber', playload[0])
			Vue.set(state.tabledetail, 'personnum', playload[1])
			Vue.set(state.tabledetail, 'tablefee', playload[2])
			// state.tabledetail.personnum=playload[1]
		},
		CLEAN_DISHES(state, playload) {
			state.orderList = []
			state.orderData = {
				count: 0,
				money: 0
			}
			// uni.setStorageSync('cleardish',JSON.stringify(playload))
			// uni.setStorageSync('orderList', JSON.stringify(state.orderList))
			uni.removeTabBarBadge({
				index: 1
			})
			let account = JSON.parse(uni.getStorageSync('currentUser')).account
			// uni.removeStorageSync(account+'orderList')
			// uni.removeStorageSync(account+'orderData')
			uni.removeStorageSync(account+'orderList')
			uni.removeStorageSync(account+'orderData')
		},
		// 查询订单列表有无 相同口味和菜式 的索引
		GET_DISHESINDEX(state, playload) {
			state.dishesIndex = null;
			for (let index = 0; index < state.orderList.length; index++) {
				if (state.orderList[index]._id == playload._id && state.orderList[index].type == playload.type) {
					state.dishesIndex = index;
					break;
				}
			}
		},
		SET_COUNT(state, playload) {
			// 订单列表无该类型索引 则添加到订单列表中
			if (state.dishesIndex == null) {
				state.orderData.count++;
				state.orderData.money += playload.row.nowprice * 1;
				state.orderList.push(playload.row);
				let a = state.orderList.length + '';
				uni.setTabBarBadge({
					index: 1,
					text: a
				})
				uni.setStorageSync('orderData', JSON.stringify(state.orderData))
				uni.setStorageSync('orderList', JSON.stringify(state.orderList))
				let account = JSON.parse(uni.getStorageSync('currentUser')).account
				uni.setStorageSync(account+'orderList',uni.getStorageSync('orderList'))
				uni.setStorageSync(account+'orderData',uni.getStorageSync('orderData'))
				return
			}
			if (playload.type) {
				// 数量 +1
				state.orderList[state.dishesIndex].count++;
				state.orderData.money += state.orderList[state.dishesIndex].nowprice * 1;
				uni.setStorageSync('orderList', JSON.stringify(state.orderList))
				uni.setStorageSync('orderData', JSON.stringify(state.orderData))
				let account = JSON.parse(uni.getStorageSync('currentUser')).account
				uni.setStorageSync(account+'orderList',uni.getStorageSync('orderList'))
				uni.setStorageSync(account+'orderData',uni.getStorageSync('orderData'))
			} else {
				// 数量 -1
				state.orderList[state.dishesIndex].count--;
				state.orderData.money -= state.orderList[state.dishesIndex].nowprice * 1;
				// 减至数量为0时，删掉该订单
				if (state.orderList[state.dishesIndex].count == 0) {
					state.orderData.count--;
					state.orderList.splice(state.dishesIndex, 1);
				}
				uni.setStorageSync('orderList', JSON.stringify(state.orderList))
				uni.setStorageSync('orderData', JSON.stringify(state.orderData))
				let account = JSON.parse(uni.getStorageSync('currentUser')).account
				uni.setStorageSync(account+'orderList',uni.getStorageSync('orderList'))
				uni.setStorageSync(account+'orderData',uni.getStorageSync('orderData'))
			}
		}
	},
	actions: {
		CHANGE_COUNT({
			commit
		}, playload) {
			commit('GET_DISHESINDEX', playload.row);
			commit('SET_COUNT', playload);
		},
		CLEAN_DISHES({
			commit
		}, playload) {
			uni.showModal({
				title: '确定清空购物车吗？',
				success: function(res) {
					if (res.confirm) {
						commit('CLEAN_DISHES')
					}
				}
			});
		}
	}
})

export default store
